package com.vin.dubbo.examples;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.ClusterRules;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <br>
 *
 * @author levin
 * @version 1.0.0
 */
@Slf4j
@DubboService(cluster = ClusterRules.FAIL_FAST, timeout = 5000)
//@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean insertUser(UserDTO userDTO) {
        User target = new User();
        BeanUtils.copyProperties(userDTO, target);
        if (null != userDTO.getSleep()) {
            try {
                TimeUnit.SECONDS.sleep(userDTO.getSleep());
                log.info("thread {} sleep {}s", Thread.currentThread().getName(), userDTO.getSleep());
            } catch (InterruptedException e) {
                log.warn(Thread.currentThread().getName() + " interrupted.");
            }
        }
        int insert = userMapper.insert(target);
        log.info("insert user {} success.", userDTO.getAccName());
        return insert > 0;
    }
}
